#include "MwadjProcessor.h"
#include "conePlotter.h"
#include <boost/graph/breadth_first_search.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/graph/properties.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/ref.hpp>

#include <boost/graph/planar_canonical_ordering.hpp>
#include <boost/graph/is_straight_line_drawing.hpp>
#include <boost/graph/chrobak_payne_drawing.hpp>
#include <boost/graph/connected_components.hpp>

#define BOOST_GRAPH_PREFER_STD_LIB

#include <boost/graph/make_connected.hpp>
#include <boost/graph/make_biconnected_planar.hpp>
#include <boost/graph/make_maximal_planar.hpp>
#include <boost/graph/boyer_myrvold_planar_test.hpp>

#include <math.h>
#include <dirent.h>
#include <limits.h>
#include <float.h>

using namespace std;
using namespace boost;

MwadjProcessor::MwadjProcessor(void)
{
	this->overall_ticks = 0;
	this->no_of_edges = 0;
	this->no_of_vertices = 0;
	mySystem firstSysnode;
	firstSysnode.acceleration = 0.0;
	firstSysnode.acc_chi_val =0;
	firstSysnode.chi_val = 0;
	firstSysnode.firing_rate =0.0;
	firstSysnode.freq =0;
	firstSysnode.sbp=0.0;
	firstSysnode.tick_time = 0;
	this->system_properties.insert(pair<double,mySystem>(0.0,firstSysnode));
	this->mytempCone.basicConegen();
	this->myColormap.generateColorMap();
	this->min_chi_vertex = INT_MAX;
	this->max_chi_vertex = INT_MIN;
	this->min_chi_edge = INT_MAX;
	this->max_freq_vertex= INT_MIN;
	this->min_freq_vertex= INT_MAX;
	this->max_freq_edge= INT_MIN;
	this->min_freq_edge= INT_MAX;
	this->max_sbp_vertex= FLT_MIN;
	this->min_sbp_vertex = FLT_MAX;
	this->max_sbp_edge= FLT_MIN;
	this->min_sbp_edge= FLT_MAX;
	this->no_of_edge_replacements=0;
	this->no_of_new_edge_added = 0;
	this->mycurl_edge.setopfilename(this->ipdir+"\\events_1_edges.rec");
	this->mycurl_vertex.setopfilename(this->ipdir+"\\events_1_vertex.rec");
	this->mycurl_graph.setopfilename(this->ipdir+"\\events_1_MST.rec");
	this->mycurl_entire_graph.setopfilename(this->ipdir+"\\events_1_graph.rec");
	this->edgemetadetailspointer.open(this->ipdir+"\\edgerates.meta");
	this->edgemetadetailspointer<<"ticktime\ttimestamp\tno_of_edge_replacements\tno_of_new_edges"<<endl;

}

MwadjProcessor::~MwadjProcessor(void)
{
	this->edgemetadetailspointer.close();
}

void MwadjProcessor :: processinput(string filename)
{
	std::ifstream ipfilepointer;
	ipfilepointer.open(filename.c_str(),ifstream::in);
	string linetoread;
	int global_vertex_id = this->no_of_vertices;
	int global_edge_id = this->no_of_edges; 
	//this->time_sorted_mwadj.clear();
	while(getline(ipfilepointer,linetoread))
	{
		vector<string> adj_edges;
		boost::split(adj_edges,linetoread,boost::is_any_of(":"));

		int vertex_u = atoi(adj_edges[0].c_str());

		int newer_vertex_u,newer_vertex_v;

		//***********************************************************************************************
		//the vertex id mapping is used to make sure that the vertex ids are all sequential. 
		//***********************************************************************************************
		map<int,int>::iterator to_find_new_id_u = this->vertex_id_mapping.find(vertex_u);		
		if ( to_find_new_id_u == this->vertex_id_mapping.end())
		{
			vertex_id_mapping.insert(pair<int,int>(vertex_u,global_vertex_id));
			newer_vertex_u = global_vertex_id;
			global_vertex_id++;
		}
		else
		{
			newer_vertex_u = to_find_new_id_u->second;
		}

		boost::trim_right_if(adj_edges[1],boost ::is_any_of(";"));
		boost::trim_right_if(adj_edges[1],boost ::is_any_of(","));

		vector<string> list_of_edges;
		boost::split(list_of_edges,adj_edges[1],boost::is_any_of(")"));
		
		for ( vector<string>::iterator itr = list_of_edges.begin();itr != list_of_edges.end(); itr++)
		{
			if ( *itr == "")
				continue;
			vector<string> split_v_times;
			boost ::split(split_v_times,*itr,boost::is_any_of("(")); 
			boost ::trim(split_v_times[0]);
			boost ::trim_left_if(split_v_times[0],boost::is_any_of(","));
			int vertex_v = atoi(split_v_times[0].c_str());

			/*************************************************************************
			to make sure the vertex ids are sequential
			*************************************************************************/
			map<int,int>::iterator to_find_new_id_v =  this->vertex_id_mapping.find(vertex_v);
			 if (to_find_new_id_v == this->vertex_id_mapping.end())
			 {
				 this->vertex_id_mapping.insert(pair<int,int>(vertex_v,global_vertex_id));
				 newer_vertex_v = global_vertex_id;
				 global_vertex_id++;

			 }
			 else
			 {
				 newer_vertex_v = to_find_new_id_v->second;
			 }
			
			/*****************************************************************************
			this needs a little bit of fix up.. need to find out whether the vertex combinations
			should be of the newer vertex ids or that of the old ones --- this is done and fixed.
			*******************************************************************************/


			/*string end_vertexes_1 = adj_edges[0] + "," + split_v_times[0];
			string end_vertexes_2 = split_v_times[0] + "," + adj_edges[0];*/
			string end_vertexes_1 = boost::lexical_cast<string>(newer_vertex_u)+","+boost::lexical_cast<string>(newer_vertex_v);
			string end_vertexes_2 = boost::lexical_cast<string>(newer_vertex_v)+","+boost::lexical_cast<string>(newer_vertex_u);

			map<string,int>::iterator edge_id_map_itr = edge_id_mapping.find(end_vertexes_1);
			if ( edge_id_map_itr == edge_id_mapping.end())
			{
				//this means the edge occurs for the first time. add to edge_id_mapping and also to the main graph.
				edge_id_mapping.insert(pair<string,int>(end_vertexes_1,global_edge_id));
				edge_id_mapping.insert(pair<string,int>(end_vertexes_2,global_edge_id));
				
				vector<string> timestamps;
				boost :: split(timestamps,split_v_times[1],boost::is_any_of(","));
				for ( vector<string>::iterator sub_itr = timestamps.begin();sub_itr!=timestamps.end(); sub_itr++)
				{
					double curr_timestamp = boost ::lexical_cast<double>(*sub_itr);
					map<double,vector<string> >::iterator time_itr = time_sorted_mwadj.find(curr_timestamp);
					if ( time_itr != time_sorted_mwadj.end())
					{
						time_itr->second.push_back(end_vertexes_1);
					
					}
					else
					{
						vector<string> new_edge_list;
						
						new_edge_list.push_back(end_vertexes_1);
						time_sorted_mwadj.insert(pair<double,vector<string> >(curr_timestamp,new_edge_list));
						//delete &new_edge_list;
						
					}
					//delete &time_itr;

				}
				global_edge_id++;
				//delete &timestamps;
			}
			//else condition is to handle directed graphs. cos ( u,v) does not cover (v,u) in directed wadj format. (u,v) is same as (v,u) in undirected wadj format. so if u handle (u,v) you dont have to handle (v,u).
			else
			{
				vector<string> timestamps;
				boost :: split(timestamps,split_v_times[1],boost::is_any_of(","));
				for ( vector<string>::iterator sub_itr = timestamps.begin();sub_itr!=timestamps.end(); sub_itr++)
				{
					double curr_timestamp = boost ::lexical_cast<double>(*sub_itr);
					map<double,vector<string> >::iterator time_itr = time_sorted_mwadj.find(curr_timestamp);
					if ( time_itr != time_sorted_mwadj.end())
					{
						time_itr->second.push_back(end_vertexes_2);
					
					}
					else
					{
						vector<string> new_edge_list;
						
						new_edge_list.push_back(end_vertexes_2);
						time_sorted_mwadj.insert(pair<double,vector<string> >(curr_timestamp,new_edge_list));
						//delete &new_edge_list;
						
					}
					//delete &time_itr;

				}
				
			}

		//delete &split_v_times;
		//delete &to_find_new_id_v;
		//delete &edge_id_map_itr;
		}
	/*delete &adj_edges;
	delete &list_of_edges;
	delete &to_find_new_id_u;*/
	
	}
	this->no_of_vertices = global_vertex_id;
	this->no_of_edges = global_edge_id;
	
	
}

template<typename myDataType> 
int MwadjProcessor::compareFunction(myDataType first_val,myDataType sec_val)
{
	return first_val>=sec_val ? 1 : -1;
}

template<typename param1Type, typename param2Type>
float MwadjProcessor::calculate_sbp(param1Type param1, param2Type param2)
{
	
	 return (float)sqrt(2*param2*(std::log((double)(2*param1))/std::log(2.0)));
	 //return (float)sqrt(2*param2*(std::log((double)(param1))/std::log(2.0)));
}
/***********************************************************************************************
Function : ProcessSystem
Input: sysFreq --- No of activities that occured at one timeinstance
	   tickno --- the tick number corresponding to the timeinstance
	   timestamp --- the actual timestamp corresponding to the timeinstance
Output: vector<chiValType> ---- list of chi_vals depending on the number of comparisons to be done. 
Description: This function calculates the system details at a given point of time and updates the 
			system properties map accordingly. 
*************************************************************************************************/
template <typename sysFreqType,typename tickNoType,typename chiValType,typename timestampType,typename sbpType >
void MwadjProcessor::processSystem(sysFreqType sysFreq, tickNoType tickno, timestampType timestamp, vector<chiValType>& listofchis,sbpType& sbp)
{
	map<double,mySystem>::reverse_iterator prev_sys_itr = this->system_properties.rbegin();
	mySystem currSysDetails;
	currSysDetails.tick_time = tickno;
	currSysDetails.freq = prev_sys_itr->second.freq + sysFreq;
	currSysDetails.firing_rate = currSysDetails.freq/currSysDetails.tick_time;
	currSysDetails.acceleration = (currSysDetails.firing_rate - prev_sys_itr->second.firing_rate)/(currSysDetails.tick_time-prev_sys_itr->second.tick_time);
	currSysDetails.chi_val = this->compareFunction(currSysDetails.firing_rate,prev_sys_itr->second.firing_rate);
	currSysDetails.acc_chi_val = this->compareFunction(currSysDetails.acceleration,prev_sys_itr->second.acceleration);
	sbp = this->calculate_sbp(currSysDetails.freq,currSysDetails.tick_time);
	currSysDetails.sbp = sbp;
	this->system_properties.insert(pair<double,mySystem>(timestamp,currSysDetails));
	listofchis.push_back(currSysDetails.chi_val);
	listofchis.push_back(currSysDetails.acc_chi_val);
	//delete &prev_sys_itr;
	/*map<string,string> sys_node_prop;
	sys_node_prop.insert(pair<string,string>("size","10"));
	sys_node_prop.insert(pair<string,string>("x",boost::lexical_cast<std::string>(tickno)));
	sys_node_prop.insert(pair<string,string>("y",boost::lexical_cast<std::string>(sbp)));
	sys_node_prop.insert(pair<string,string>("y",boost::lexical_cast<std::string>(sbp)));
	sys_node_prop.insert(pair<string,string>("r","0"));
	sys_node_prop.insert(pair<string,string>("g","0"));
	sys_node_prop.insert(pair<string,string>("b","0"));
	this->mycurl.add_node(30000,sys_node_prop);*/

	return;

	
}
template<typename param1Type, typename param2Type>
float MwadjProcessor::calc_edge_disc_wt(param1Type param1,param2Type param2)
{
	return abs(abs(param1)-param2);
}
template<typename xType,typename yType>
int MwadjProcessor::calAngle(xType x,yType y)
{
	return (int)floor((atan((float)x/(float)y)*180)/PI);
}
template<typename edgeElement,typename ticktimeType, typename timestampType,typename chiValType,typename sbpType> 
void MwadjProcessor:: processEdge(edgeElement id,ticktimeType ticktime,timestampType timestamp, vector<chiValType>& listofSysChis,sbpType sbp,int src,int tar)
{
	bool sendFlag = true;
	map<int,myEdge>::iterator tempEdge_itr = this->edge_properties.find(id);
	int tempChi;
	float tempSbp;
	int tempFreq;
	if ( tempEdge_itr == this->edge_properties.end())
	{
		myEdge newEdge;
		newEdge.lastticktime = ticktime;
		newEdge.lasttimestamp = timestamp;
		newEdge.freq = 1;
		newEdge.firing_rate = (float)newEdge.freq/(float)newEdge.lastticktime;
		newEdge.acceleration = newEdge.firing_rate/(float)newEdge.lastticktime;
		newEdge.chi_val = listofSysChis[0];
		newEdge.acc_chi_val = listofSysChis[1];
		newEdge.disc_weight = this->calc_edge_disc_wt(newEdge.chi_val,sbp);
		newEdge.sbp = this->calculate_sbp(newEdge.freq,newEdge.lastticktime);
		this->edge_properties.insert(pair<int,myEdge>(id,newEdge));
		tempChi = newEdge.chi_val;
		tempSbp = newEdge.sbp;
		tempFreq = newEdge.freq;			

	}
	else
	{
		float prev_firing_rate = tempEdge_itr->second.firing_rate;
		float prev_acceleration = tempEdge_itr->second.acceleration;
		int prev_ticktime = tempEdge_itr->second.lastticktime;
		double prev_timestamp = tempEdge_itr->second.lasttimestamp;

		tempEdge_itr->second.lastticktime = ticktime;
		tempEdge_itr->second.lasttimestamp = timestamp;
		tempEdge_itr->second.freq++;
		tempEdge_itr->second.sbp = this->calculate_sbp(tempEdge_itr->second.freq,tempEdge_itr->second.lastticktime);
		tempEdge_itr->second.firing_rate = (float)tempEdge_itr->second.freq/(float)tempEdge_itr->second.lastticktime;
		tempEdge_itr->second.acceleration = (tempEdge_itr->second.firing_rate - prev_firing_rate)/(float)(tempEdge_itr->second.lastticktime - prev_ticktime);

		if((( tempEdge_itr->second.firing_rate >= prev_firing_rate) && (listofSysChis[0] == 1)) || ((tempEdge_itr->second.firing_rate < prev_firing_rate) && (listofSysChis[0] == -1)))
			tempEdge_itr->second.chi_val++;
		else
			tempEdge_itr->second.chi_val--;

		if (((tempEdge_itr->second.acceleration >= prev_acceleration) && (listofSysChis[1] == 1)) || ((tempEdge_itr->second.acceleration < prev_acceleration ) && ( listofSysChis[1] == -1)))
			tempEdge_itr->second.acc_chi_val++;
		else
			tempEdge_itr->second.acc_chi_val--;

		tempEdge_itr->second.disc_weight = this->calc_edge_disc_wt(tempEdge_itr->second.chi_val,sbp);
		tempChi = tempEdge_itr->second.chi_val;
		tempSbp = tempEdge_itr->second.sbp;
		tempFreq = tempEdge_itr->second.freq;
	
	}
	//delete &tempEdge_itr;
		if ( tempChi > this->max_chi_edge)
			this->max_chi_edge = tempChi;
		if (tempChi < this->min_chi_edge)
			this->min_chi_edge = tempChi;
		if ( tempFreq > this->max_freq_edge)
			this->max_freq_edge = tempFreq;
		if ( tempFreq < this->min_freq_edge)
			this->min_freq_edge = tempFreq;
		if ( tempSbp > this -> max_sbp_edge)
			this->max_sbp_edge = tempSbp;
		if ( tempSbp < this->min_sbp_edge)
			this->min_sbp_edge = tempSbp;
	if ( sendFlag)
	{
		map<string,string> edge_prop;
		int edge_angle = (this->calAngle(abs(tempChi),tempSbp));
		colorMapper::rgb edge_rgb;
		if ( edge_angle<=45 && edge_angle>=0)
			edge_rgb = this->myColormap.getColor(0,45,edge_angle);
		else
		{
			edge_rgb.b =0;
			edge_rgb.g = 0;
			edge_rgb.r =0;
		}
		map<float,sbp_det_struct>::iterator sbp_val_itr = this->sbp_list_edge.find(tempSbp);
		if ( sbp_val_itr == this->sbp_list_edge.end())
		{
			MwadjProcessor::sbp_det_struct tempSbpstruct;
			tempSbpstruct.min_chi = tempChi;
			tempSbpstruct.max_chi = tempChi;
			tempSbpstruct.min_id = id;
			tempSbpstruct.max_id = id;
			this->sbp_list_edge.insert(pair<float,sbp_det_struct>(tempSbp,tempSbpstruct));
			if ( tempChi != 1)
				edge_prop.insert(pair<string,string>("label",boost::lexical_cast<std::string>(id)));
		}
		else
		{
			if ( tempChi < sbp_val_itr->second.min_chi )
			{
				sbp_val_itr->second.min_chi = tempChi;
				if (( sbp_val_itr->second.min_id != id ) && ( sbp_val_itr->second.min_id != sbp_val_itr->second.max_id ))
				{
					map<string,string> temp_prop;
					temp_prop.insert(pair<string,string>("label",""));
					this->mycurl_edge.change_node(sbp_val_itr->second.min_id ,temp_prop);
				}
				sbp_val_itr->second.min_id = id;
				if ( tempChi != 1)
					edge_prop.insert(pair<string,string>("label",boost::lexical_cast<std::string>(id)));
			}
			else if ( tempChi > sbp_val_itr->second.max_chi)
			{
				sbp_val_itr->second.max_chi = tempChi;
				if (( sbp_val_itr->second.min_id != id ) && ( sbp_val_itr->second.min_id != sbp_val_itr->second.max_id ))
				{
					map<string,string> temp_prop;
					temp_prop.insert(pair<string,string>("label",""));
					this->mycurl_edge.change_node(sbp_val_itr->second.min_id ,temp_prop);
				}
				sbp_val_itr->second.max_id = id;
				if ( tempChi != 1)
					edge_prop.insert(pair<string,string>("label",boost::lexical_cast<std::string>(id)));
			}
			else
			{
				edge_prop.insert(pair<string,string>("label",""));
			}

		}

		edge_prop.insert(pair<string,string>("src",boost::lexical_cast<std::string>(src)));
		edge_prop.insert(pair<string,string>("tar",boost::lexical_cast<std::string>(tar)));
		edge_prop.insert(pair<string,string>("x",boost::lexical_cast<std::string>(tempChi+0.1)));
		edge_prop.insert(pair<string,string>("chi_val",boost::lexical_cast<std::string>(tempChi)));
		edge_prop.insert(pair<string,string>("freq",boost::lexical_cast<std::string>(tempFreq)));
		edge_prop.insert(pair<string,string>("y",boost::lexical_cast<std::string>(tempSbp+0.1)));
		edge_prop.insert(pair<string,string>("size","2"));
		edge_prop.insert(pair<string,string>("r",boost::lexical_cast<std::string>((float)edge_rgb.r/255)));
		edge_prop.insert(pair<string,string>("g",boost::lexical_cast<std::string>((float)edge_rgb.g/255)));
		edge_prop.insert(pair<string,string>("b",boost::lexical_cast<std::string>((float)edge_rgb.b/255)));
		this->mycurl_edge.add_node(id,edge_prop);

	}
	return;

}
template<typename vertexElement,typename ticktimeType,typename chiValType,typename timestampType,typename sbpType> 
void MwadjProcessor::processVertex(vertexElement id,ticktimeType ticktime,timestampType timestamp,vector<chiValType>& listofchis,sbpType sbp)
{
	bool sendFlag = true;
	map<int,myVertex>::iterator tempVertex_itr = this->vertex_properties.find(id);
	int tempChi;
	float tempSbp;
	int tempFreq;
	if ( tempVertex_itr == this->vertex_properties.end())
	{
		myVertex newVertex;
		newVertex.lastticktime = ticktime;
		newVertex.lasttimestamp = timestamp;
		newVertex.freq = 1;
		newVertex.firing_rate = (float)newVertex.freq/(float)newVertex.lastticktime;
		newVertex.acceleration = (float)newVertex.firing_rate/(float)newVertex.lastticktime;
		newVertex.chi_val = listofchis[0];
		newVertex.acc_chi_val = listofchis[1];
		newVertex.sbp = this->calculate_sbp(newVertex.freq,newVertex.lastticktime);
		this->vertex_properties.insert(pair<int,myVertex>(id,newVertex));
		tempChi = newVertex.chi_val;
		tempFreq = newVertex.freq;
		tempSbp = newVertex.sbp;
	}
	else
	{
		float prev_firing_rate = tempVertex_itr->second.firing_rate;
		float prev_acceleration = tempVertex_itr->second.acceleration;
		int prev_ticktime = tempVertex_itr->second.lastticktime;
		
		tempVertex_itr->second.lastticktime  = ticktime;
		tempVertex_itr->second.lasttimestamp = timestamp;
		tempVertex_itr->second.freq++;
		tempVertex_itr->second.sbp = this->calculate_sbp(tempVertex_itr->second.freq,tempVertex_itr->second.lastticktime);
		tempVertex_itr->second.firing_rate = (float)tempVertex_itr->second.freq/(float)tempVertex_itr->second.lastticktime;
		tempVertex_itr->second.acceleration = (float)(tempVertex_itr->second.firing_rate - prev_firing_rate)/(float)(tempVertex_itr->second.lastticktime - prev_ticktime);
		if (((tempVertex_itr->second.firing_rate >= prev_firing_rate)&&(listofchis[0] == 1)) || ((tempVertex_itr->second.firing_rate < prev_firing_rate) && (listofchis[0] == -1)))
			tempVertex_itr->second.chi_val++;
		else
			tempVertex_itr->second.chi_val--;

		if (((tempVertex_itr->second.acceleration >= prev_acceleration) && (listofchis[1] == 1)) || ((tempVertex_itr->second.acceleration < prev_acceleration) && (listofchis[1] == -1 )))
			tempVertex_itr->second.acc_chi_val++;
		else
			tempVertex_itr->second.acc_chi_val--;
		tempChi = tempVertex_itr->second.chi_val;
		tempFreq = tempVertex_itr->second.freq;
		tempSbp = tempVertex_itr->second.sbp;
		
	}
	//delete &tempVertex_itr;
	if ( tempChi > this->max_chi_vertex)
		this->max_chi_vertex = tempChi;
	if (tempChi < this->min_chi_vertex)
		this->min_chi_vertex = tempChi;
	if ( tempFreq > this->max_freq_vertex)
		this->max_freq_vertex = tempFreq;
	if ( tempFreq < this->min_freq_vertex)
		this->min_freq_vertex = tempFreq;
	if ( tempSbp > this -> max_sbp_vertex)
		this->max_sbp_vertex = tempSbp;
	if ( tempSbp < this->min_sbp_vertex)
		this->min_sbp_vertex = tempSbp;
	

	if ( sendFlag)
	{
		map<string,string> vertex_prop;
		int vertex_angle = (this->calAngle(abs(tempChi),tempSbp));
		colorMapper::rgb vertex_rgb;
		if ( vertex_angle<=45 && vertex_angle>=0)
			vertex_rgb = this->myColormap.getColor(0,45,vertex_angle);
		else
		{
			vertex_rgb.b =0;
			vertex_rgb.g = 0;
			vertex_rgb.r =0;
		}
		map<float,sbp_det_struct>::iterator sbp_val_itr = this->sbp_list_vertex.find(tempSbp);
		if ( sbp_val_itr == this->sbp_list_vertex.end())
		{
			MwadjProcessor::sbp_det_struct tempSbpstruct;
			tempSbpstruct.min_chi = tempChi;
			tempSbpstruct.max_chi = tempChi;
			tempSbpstruct.min_id = id;
			tempSbpstruct.max_id = id;
			this->sbp_list_vertex.insert(pair<float,sbp_det_struct>(tempSbp,tempSbpstruct));
			if ( tempChi != 1)
				vertex_prop.insert(pair<string,string>("label",boost::lexical_cast<std::string>(id)));
		}
		else
		{
			if ( tempChi < sbp_val_itr->second.min_chi )
			{
				sbp_val_itr->second.min_chi = tempChi;
				if (( sbp_val_itr->second.min_id != id )&& ( sbp_val_itr->second.min_id != sbp_val_itr->second.max_id ))
				{
					map<string,string> temp_prop;
					temp_prop.insert(pair<string,string>("label",""));
					this->mycurl_vertex.change_node(sbp_val_itr->second.min_id ,temp_prop);
				}
				sbp_val_itr->second.min_id = id;
				if ( tempChi != 1)
					vertex_prop.insert(pair<string,string>("label",boost::lexical_cast<std::string>(id)));
			}
			else if ( tempChi > sbp_val_itr->second.max_chi)
			{
				sbp_val_itr->second.max_chi = tempChi;
				if (( sbp_val_itr->second.min_id != id )&& ( sbp_val_itr->second.min_id != sbp_val_itr->second.max_id ))
				{
					map<string,string> temp_prop;
					temp_prop.insert(pair<string,string>("label",""));
					this->mycurl_vertex.change_node(sbp_val_itr->second.min_id ,temp_prop);
				}
				sbp_val_itr->second.max_id = id;
				if ( tempChi != 1)
					vertex_prop.insert(pair<string,string>("label",boost::lexical_cast<std::string>(id)));
			}
			else
			{
				vertex_prop.insert(pair<string,string>("label",""));
			}
		}
		vertex_prop.insert(pair<string,string>("x",boost::lexical_cast<std::string>(tempChi+0.1)));
		vertex_prop.insert(pair<string,string>("chi_val",boost::lexical_cast<std::string>(tempChi)));
		vertex_prop.insert(pair<string,string>("freq",boost::lexical_cast<std::string>(tempFreq)));
		vertex_prop.insert(pair<string,string>("y",boost::lexical_cast<std::string>(tempSbp+0.1)));
		vertex_prop.insert(pair<string,string>("sbp",boost::lexical_cast<std::string>(tempSbp)));
		vertex_prop.insert(pair<string,string>("size","2"));
		vertex_prop.insert(pair<string,string>("r",boost::lexical_cast<std::string>((float)vertex_rgb.r/255)));
		vertex_prop.insert(pair<string,string>("g",boost::lexical_cast<std::string>((float)vertex_rgb.g/255)));
		vertex_prop.insert(pair<string,string>("b",boost::lexical_cast<std::string>((float)vertex_rgb.b/255)));
		this->mycurl_vertex.add_node(id,vertex_prop);

	}
	return;
}
void MwadjProcessor ::initialiseDisjointSets(mydisjointSet& ds)
{
	for ( map<int,int>::iterator all_vertices_itr = this->vertex_id_mapping.begin();all_vertices_itr != this->vertex_id_mapping.end();all_vertices_itr++)
	{
		ds.make_set(all_vertices_itr->second);
	}

	return;
}
template<typename vertexElement> 
void MwadjProcessor ::updateInputGraph(vertexElement src,vertexElement tar ,bool sendFlag)
{
	string main_comb_vertices = boost::lexical_cast<std::string>(src)+","+boost::lexical_cast<std::string>(tar);
	int main_edge_id = this->edge_id_mapping.find(main_comb_vertices)->second;
	
	
	float edge_disc_weight = this->edge_properties.find(main_edge_id)->second.disc_weight;
	float edge_firing_rate = this->edge_properties.find(main_edge_id)->second.firing_rate;
	int edge_chi_value = abs(this->edge_properties.find(main_edge_id)->second.chi_val);
	int edge_ticktime = this->edge_properties.find(main_edge_id)->second.lastticktime;
	int edge_freq = this->edge_properties.find(main_edge_id)->second.freq;
	float edge_sbp = this->edge_properties.find(main_edge_id)->second.sbp;
	
	float src_firing_rate = this->vertex_properties.find(src)->second.firing_rate*1000;
	float tar_firing_rate = this->vertex_properties.find(tar)->second.firing_rate*1000;

	float src_sbp = this->vertex_properties.find(src)->second.sbp;
	float tar_sbp = this->vertex_properties.find(tar)->second.sbp;
	
	

	int src_freq = this->vertex_properties.find(src)->second.freq;
	int tar_freq = this->vertex_properties.find(tar)->second.freq;

	int src_chi = (this->vertex_properties.find(src)->second.chi_val);
	int tar_chi = (this->vertex_properties.find(tar)->second.chi_val);

	colorMapper::rgb src_rgb;
	colorMapper::rgb tar_rgb;
	colorMapper::rgb edge_rgb;

	
	int src_angle = (this->calAngle(abs(src_chi),src_sbp));
	int tar_angle = (this->calAngle(abs(tar_chi),tar_sbp));
	int edge_angle = (this->calAngle(abs(edge_chi_value),edge_sbp));

	if ( src_angle<=45 && src_angle>=0)
		src_rgb = this->myColormap.getColor(0,45,src_angle);
	else
	{
		src_rgb.b =0;
		src_rgb.g=0;
		src_rgb.r =0;
	}
	if ( tar_angle<=45 && tar_angle>=0)

		tar_rgb = this->myColormap.getColor(0,45,tar_angle);
	else
	{
		tar_rgb.b =0;
		tar_rgb.g = 0;
		tar_rgb.r =0;
	}

	if ( edge_angle<=45 && edge_angle>=0)
		edge_rgb = this->myColormap.getColor(0,45,edge_angle);
	else
	{
		edge_rgb.b =0;
		edge_rgb.g = 0;
		edge_rgb.r =0;
	}

	map<string,string> src_prop,tar_prop,edge_prop;

	//map<int,XYZcoord>::iterator src_xy_itr= this->nodeXYZ.find(src);
	//map<int,XYZcoord>::iterator tar_xy_itr= this->nodeXYZ.find(tar);
	/*float src_ratio = 1 + (log10((float)this->mytempCone.max_slot_no)-log10((float)src_slot))/log10(2.0);
	float tar_ratio = 1 + (log10((float)this->mytempCone.max_slot_no)-log10((float)tar_slot))/log10(2.0);*/
	float src_size = 10;//(log10(20000.0)/log10(2.0)+src_slot)*src_ratio;
	float tar_size = 10;//(log10(20000.0)/log10(2.0)+tar_slot)*tar_ratio;
	/*	string src_label = this->nodeLabel.find(src)->second;
	string tar_label = this->nodeLabel.find(tar)->second;*/
	//src_prop.insert(pair<string,string>("size",boost::lexical_cast<std::string>(20+pow(log10((float)src_slot*10),3))));
	//src_prop.insert(pair<string,string>("Label",src_label));
	src_prop.insert(pair<string,string>("size",boost::lexical_cast<std::string>(src_size)));
	src_prop.insert(pair<string,string>("angle",boost::lexical_cast<std::string>(src_angle)));
	src_prop.insert(pair<string,string>("ver_freq",boost::lexical_cast<std::string>(src_freq)));
	src_prop.insert(pair<string,string>("chi_val",boost::lexical_cast<std::string>(src_chi)));
	src_prop.insert(pair<string,string>("sbp",boost::lexical_cast<std::string>(src_sbp)));
	//src_prop.insert(pair<string,string>("x",boost::lexical_cast<std::string>(src_xy_itr->second.X)));
	//src_prop.insert(pair<string,string>("y",boost::lexical_cast<std::string>(src_xy_itr->second.Y)));
	/*src_prop.insert(pair<string,string>("x",boost::lexical_cast<std::string>(src_chi*10)));
	src_prop.insert(pair<string,string>("y",boost::lexical_cast<std::string>(src_freq*10)));*/

	/*src_prop.insert(pair<string,string>("x",boost::lexical_cast<std::string>(src_chi)));
	src_prop.insert(pair<string,string>("y",boost::lexical_cast<std::string>(src_sbp)));*/

	src_prop.insert(pair<string,string>("r",boost::lexical_cast<std::string>((float)src_rgb.r/255)));
	src_prop.insert(pair<string,string>("g",boost::lexical_cast<std::string>((float)src_rgb.g/255)));
	src_prop.insert(pair<string,string>("b",boost::lexical_cast<std::string>((float)src_rgb.b/255)));

	//tar_prop.insert(pair<string,string>("Label",tar_label));
	tar_prop.insert(pair<string,string>("size",boost::lexical_cast<std::string>(tar_size)));
	tar_prop.insert(pair<string,string>("angle",boost::lexical_cast<std::string>(tar_angle)));
	tar_prop.insert(pair<string,string>("ver_freq",boost::lexical_cast<std::string>(tar_freq)));
	tar_prop.insert(pair<string,string>("chi_val",boost::lexical_cast<std::string>(tar_chi)));
	tar_prop.insert(pair<string,string>("sbp",boost::lexical_cast<std::string>(tar_sbp)));
	/*tar_prop.insert(pair<string,string>("x",boost::lexical_cast<std::string>(tar_xy_itr->second.X)));
	tar_prop.insert(pair<string,string>("y",boost::lexical_cast<std::string>(tar_xy_itr->second.Y)));*/
	/*tar_prop.insert(pair<string,string>("x",boost::lexical_cast<std::string>(tar_chi*10)));
	tar_prop.insert(pair<string,string>("y",boost::lexical_cast<std::string>(tar_freq*10)));*/

	//tar_prop.insert(pair<string,string>("x",boost::lexical_cast<std::string>(tar_chi)));
	//tar_prop.insert(pair<string,string>("y",boost::lexical_cast<std::string>(tar_sbp)));

	tar_prop.insert(pair<string,string>("r",boost::lexical_cast<std::string>((float)tar_rgb.r/255)));
	tar_prop.insert(pair<string,string>("g",boost::lexical_cast<std::string>((float)tar_rgb.g/255)));
	tar_prop.insert(pair<string,string>("b",boost::lexical_cast<std::string>((float)tar_rgb.b/255)));


	edge_prop.insert(pair<string,string>("weight",boost::lexical_cast<std::string>(edge_sbp)));
	//edge_prop.insert(pair<string,string>("start",boost::lexical_cast<std::string>(edge_ticktime)));
	edge_prop.insert(pair<string,string>("firing_rate",boost::lexical_cast<std::string>(edge_firing_rate)));
	edge_prop.insert(pair<string,string>("discrepancy_weight",boost::lexical_cast<std::string>(edge_disc_weight)));
	edge_prop.insert(pair<string,string>("source",boost::lexical_cast<std::string>(src)));
	edge_prop.insert(pair<string,string>("target",boost::lexical_cast<std::string>(tar)));
	edge_prop.insert(pair<string,string>("edge_freq",boost::lexical_cast<std::string>(edge_freq)));
	edge_prop.insert(pair<string,string>("edge_chi_val",boost::lexical_cast<std::string>(edge_chi_value)));
	edge_prop.insert(pair<string,string>("edge_sbp",boost::lexical_cast<std::string>(edge_sbp)));
	edge_prop.insert(pair<string,string>("edge_angle",boost::lexical_cast<std::string>(edge_angle)));
	edge_prop.insert(pair<string,string>("r",boost::lexical_cast<std::string>((float)edge_rgb.r/255)));
	edge_prop.insert(pair<string,string>("g",boost::lexical_cast<std::string>((float)edge_rgb.g/255)));
	edge_prop.insert(pair<string,string>("b",boost::lexical_cast<std::string>((float)edge_rgb.b/255)));
	this->mycurl_entire_graph.add_edge(src,src_prop,tar,tar_prop,edge_prop);
	return;

}
template<typename vertexElement,typename disjointsetType>
void MwadjProcessor :: updateMST(vertexElement src, vertexElement tar,disjointsetType& ds,float sbp,bool sendFlag)
{
	//curlWrapper mycurl;
	if ( src == tar )
		return;
	int src_parent = ds.find_set(src);
	int tar_parent = ds.find_set(tar);
	string main_comb_vertices = boost::lexical_cast<std::string>(src)+","+boost::lexical_cast<std::string>(tar);
	int main_edge_id = this->edge_id_mapping.find(main_comb_vertices)->second;
	
	
	
	float edge_disc_weight = this->edge_properties.find(main_edge_id)->second.disc_weight;
	float edge_firing_rate = this->edge_properties.find(main_edge_id)->second.firing_rate;
	int edge_chi_value = abs(this->edge_properties.find(main_edge_id)->second.chi_val);
	int edge_ticktime = this->edge_properties.find(main_edge_id)->second.lastticktime;
	int edge_freq = this->edge_properties.find(main_edge_id)->second.freq;
	float edge_sbp = this->edge_properties.find(main_edge_id)->second.sbp;
	
	float src_firing_rate = this->vertex_properties.find(src)->second.firing_rate*1000;
	float tar_firing_rate = this->vertex_properties.find(tar)->second.firing_rate*1000;

	float src_sbp = this->vertex_properties.find(src)->second.sbp;
	float tar_sbp = this->vertex_properties.find(tar)->second.sbp;
	
	

	int src_freq = this->vertex_properties.find(src)->second.freq;
	int tar_freq = this->vertex_properties.find(tar)->second.freq;

	int src_chi = (this->vertex_properties.find(src)->second.chi_val);
	int tar_chi = (this->vertex_properties.find(tar)->second.chi_val);

	/*int src_slot = this->mytempCone.getSlotNumber((src_chi/100)+1,(src_freq/100)+1);
	int tar_slot = this->mytempCone.getSlotNumber((tar_chi/100)+1,(tar_freq/100)+1);*/

	colorMapper::rgb src_rgb;
	colorMapper::rgb tar_rgb;
	colorMapper::rgb edge_rgb;

	
	int src_angle = (this->calAngle(abs(src_chi),src_sbp));
	int tar_angle = (this->calAngle(abs(tar_chi),tar_sbp));
	int edge_angle = (this->calAngle(abs(edge_chi_value),edge_sbp));

	/*src_rgb = this->myColormap.getColor(1,this->mytempCone.max_slot_no,src_slot);
	tar_rgb = this->myColormap.getColor(1,this->mytempCone.max_slot_no,tar_slot);*/

	if ( src_angle<=45 && src_angle>=0)
		src_rgb = this->myColormap.getColor(0,45,src_angle);
	else
	{
		src_rgb.b =0;
		src_rgb.g=0;
		src_rgb.r =0;
	}
	if ( tar_angle<=45 && tar_angle>=0)

		tar_rgb = this->myColormap.getColor(0,45,tar_angle);
	else
	{
		tar_rgb.b =0;
		tar_rgb.g = 0;
		tar_rgb.r =0;
	}

	if ( edge_angle<=45 && edge_angle>=0)
		edge_rgb = this->myColormap.getColor(0,45,edge_angle);
	else
	{
		edge_rgb.b =0;
		edge_rgb.g = 0;
		edge_rgb.r =0;
	}

	map<string,string> src_prop,tar_prop,edge_prop;

	//map<int,XYZcoord>::iterator src_xy_itr= this->nodeXYZ.find(src);
	//map<int,XYZcoord>::iterator tar_xy_itr= this->nodeXYZ.find(tar);
	/*float src_ratio = 1 + (log10((float)this->mytempCone.max_slot_no)-log10((float)src_slot))/log10(2.0);
	float tar_ratio = 1 + (log10((float)this->mytempCone.max_slot_no)-log10((float)tar_slot))/log10(2.0);*/
	float src_size = 10;//(log10(20000.0)/log10(2.0)+src_slot)*src_ratio;
	float tar_size = 10;//(log10(20000.0)/log10(2.0)+tar_slot)*tar_ratio;
	
	//string src_label = this->nodeLabel.find(src)->second;
	//string tar_label = this->nodeLabel.find(tar)->second;
	//src_prop.insert(pair<string,string>("size",boost::lexical_cast<std::string>(20+pow(log10((float)src_slot*10),3))));
	//src_prop.insert(pair<string,string>("Label",src_label));
	src_prop.insert(pair<string,string>("size",boost::lexical_cast<std::string>(src_size)));
	src_prop.insert(pair<string,string>("angle",boost::lexical_cast<std::string>(src_angle)));
	src_prop.insert(pair<string,string>("ver_freq",boost::lexical_cast<std::string>(src_freq)));
	src_prop.insert(pair<string,string>("chi_val",boost::lexical_cast<std::string>(src_chi)));
	src_prop.insert(pair<string,string>("sbp",boost::lexical_cast<std::string>(src_sbp)));
	//src_prop.insert(pair<string,string>("x",boost::lexical_cast<std::string>(src_xy_itr->second.X)));
	//src_prop.insert(pair<string,string>("y",boost::lexical_cast<std::string>(src_xy_itr->second.Y)));
	/*src_prop.insert(pair<string,string>("x",boost::lexical_cast<std::string>(src_chi*10)));
	src_prop.insert(pair<string,string>("y",boost::lexical_cast<std::string>(src_freq*10)));*/

	/*src_prop.insert(pair<string,string>("x",boost::lexical_cast<std::string>(src_chi)));
	src_prop.insert(pair<string,string>("y",boost::lexical_cast<std::string>(src_sbp)));*/

	src_prop.insert(pair<string,string>("r",boost::lexical_cast<std::string>((float)src_rgb.r/255)));
	src_prop.insert(pair<string,string>("g",boost::lexical_cast<std::string>((float)src_rgb.g/255)));
	src_prop.insert(pair<string,string>("b",boost::lexical_cast<std::string>((float)src_rgb.b/255)));

	//tar_prop.insert(pair<string,string>("Label",tar_label));
	tar_prop.insert(pair<string,string>("size",boost::lexical_cast<std::string>(tar_size)));
	tar_prop.insert(pair<string,string>("angle",boost::lexical_cast<std::string>(tar_angle)));
	tar_prop.insert(pair<string,string>("ver_freq",boost::lexical_cast<std::string>(tar_freq)));
	tar_prop.insert(pair<string,string>("chi_val",boost::lexical_cast<std::string>(tar_chi)));
	tar_prop.insert(pair<string,string>("sbp",boost::lexical_cast<std::string>(tar_sbp)));
	/*tar_prop.insert(pair<string,string>("x",boost::lexical_cast<std::string>(tar_xy_itr->second.X)));
	tar_prop.insert(pair<string,string>("y",boost::lexical_cast<std::string>(tar_xy_itr->second.Y)));*/
	/*tar_prop.insert(pair<string,string>("x",boost::lexical_cast<std::string>(tar_chi*10)));
	tar_prop.insert(pair<string,string>("y",boost::lexical_cast<std::string>(tar_freq*10)));*/

	//tar_prop.insert(pair<string,string>("x",boost::lexical_cast<std::string>(tar_chi)));
	//tar_prop.insert(pair<string,string>("y",boost::lexical_cast<std::string>(tar_sbp)));

	tar_prop.insert(pair<string,string>("r",boost::lexical_cast<std::string>((float)tar_rgb.r/255)));
	tar_prop.insert(pair<string,string>("g",boost::lexical_cast<std::string>((float)tar_rgb.g/255)));
	tar_prop.insert(pair<string,string>("b",boost::lexical_cast<std::string>((float)tar_rgb.b/255)));


	edge_prop.insert(pair<string,string>("weight",boost::lexical_cast<std::string>(edge_sbp)));
	//edge_prop.insert(pair<string,string>("start",boost::lexical_cast<std::string>(edge_ticktime)));
	edge_prop.insert(pair<string,string>("firing_rate",boost::lexical_cast<std::string>(edge_firing_rate)));
	edge_prop.insert(pair<string,string>("discrepancy_weight",boost::lexical_cast<std::string>(edge_disc_weight)));
	edge_prop.insert(pair<string,string>("source",boost::lexical_cast<std::string>(src)));
	edge_prop.insert(pair<string,string>("target",boost::lexical_cast<std::string>(tar)));
	edge_prop.insert(pair<string,string>("edge_freq",boost::lexical_cast<std::string>(edge_freq)));
	edge_prop.insert(pair<string,string>("edge_chi_val",boost::lexical_cast<std::string>(edge_chi_value)));
	edge_prop.insert(pair<string,string>("edge_sbp",boost::lexical_cast<std::string>(edge_sbp)));
	edge_prop.insert(pair<string,string>("edge_angle",boost::lexical_cast<std::string>(edge_angle)));
	edge_prop.insert(pair<string,string>("r",boost::lexical_cast<std::string>((float)edge_rgb.r/255)));
	edge_prop.insert(pair<string,string>("g",boost::lexical_cast<std::string>((float)edge_rgb.g/255)));
	edge_prop.insert(pair<string,string>("b",boost::lexical_cast<std::string>((float)edge_rgb.b/255)));

	boost::graph_traits<Graph>::vertex_descriptor u;
	boost::graph_traits<Graph>::vertex_descriptor v;
	
	deg_size_type u_degree,v_degree;
	
	if(( src == 440) || (tar ==440))
		cout << "stop!"<<endl;

	if ( src_parent != tar_parent)
	{
		add_edge(src,tar,this->MSTGraph);
		/*u = vertex(src,this->MSTGraph);
		v = vertex(tar,this->MSTGraph);

		u_degree  = degree(u,this->MSTGraph);
		v_degree = degree(v,this->MSTGraph);
		
		float src_freqbydeg = (float)src_freq*20/(float)u_degree;
		float tar_freqbydeg = (float)tar_freq*20/(float)v_degree;*/

		/*src_prop.insert(pair<string,string>("size",boost::lexical_cast<std::string>(src_freqbydeg)));
		tar_prop.insert(pair<string,string>("size",boost::lexical_cast<std::string>(tar_freqbydeg)));*/

		ds.link(src_parent,tar_parent);
		this->no_of_new_edge_added++;
		//this->mycurl.add_edge(src,tar,main_firing_rate);
		if ( sendFlag)
			this->mycurl_graph.add_edge(src,src_prop,tar,tar_prop,edge_prop);
	}
	else
	{
		//find a path from src to tar. replace the heaviest edge.
		map<float,string> path;
		vector<int> parent(boost::num_vertices(this->MSTGraph));
		int src1,tar1;
		u = vertex(src,this->MSTGraph);
		v = vertex(tar,this->MSTGraph);

		u_degree  = degree(u,this->MSTGraph);
		v_degree = degree(v,this->MSTGraph);

		if ( u_degree > v_degree)
		{
			src1 = tar;
			tar1 = src;
		}
		else
		{
			src1 = src;
			tar1 = tar;
		}

		parent[src1] = src1;
		//boost::breadth_first_search(this->MSTGraph,src,boost::make_bfs_visitor(std::make_pair(boost::record_predecessors(&parent[0],boost::on_tree_edge()))));
		boost::breadth_first_search(this->MSTGraph,src1,boost::visitor(boost::make_bfs_visitor(boost::record_predecessors(&parent[0],boost::on_tree_edge()))));
		int temp_tar = tar1;
		int temp_src;
		do {
				temp_src = parent[temp_tar];
				string comb_vertices = boost::lexical_cast<std::string>(temp_src)+","+boost::lexical_cast<std::string>(temp_tar);
				int temp_edge_id = this->edge_id_mapping.find(comb_vertices)->second;
				float temp_edge_weight = this->edge_properties.find(temp_edge_id)->second.disc_weight;
				path.insert(pair<float,string>(temp_edge_weight,comb_vertices));
				temp_tar = temp_src;
		}while(temp_src != src1);

		if ( path.rbegin()->first > edge_disc_weight)
		{
			string edge_be_rem = path.rbegin()->second;
			vector<string> end_vertices;
			boost::split(end_vertices,edge_be_rem,boost::is_any_of(","));
			int final_src = boost::lexical_cast<int>(end_vertices[0]);
			int final_tar = boost::lexical_cast<int>(end_vertices[1]);
			boost::remove_edge(final_src,final_tar,this->MSTGraph);
			boost::add_edge(src,tar,this->MSTGraph);
			this->no_of_edge_replacements++;
		/*	u = vertex(src,this->MSTGraph);
			v = vertex(tar,this->MSTGraph);

			u_degree  = degree(u,this->MSTGraph);
			v_degree = degree(v,this->MSTGraph);
		
			float src_freqbydeg = (float)src_freq*20/(float)u_degree;
			float tar_freqbydeg = (float)tar_freq*20/(float)v_degree;
*/
		/*	src_prop.insert(pair<string,string>("size",boost::lexical_cast<std::string>(src_freqbydeg)));
			tar_prop.insert(pair<string,string>("size",boost::lexical_cast<std::string>(tar_freqbydeg)));*/
			if ( sendFlag)
			{
				this->mycurl_graph.remove_edge(final_src,final_tar);
			//this->mycurl.add_edge(src,tar,main_firing_rate);
				this->mycurl_graph.add_edge(src,src_prop,tar,tar_prop,edge_prop);
			}
			
		}
		else
		{
		 if ( sendFlag)
			if (this->mycurl_graph.remove_edge(src,tar))
				//this->mycurl.add_edge(src,tar,main_firing_rate);
			{
			/*	u = vertex(src,this->MSTGraph);
				v = vertex(tar,this->MSTGraph);

				u_degree  = degree(u,this->MSTGraph);
				v_degree = degree(v,this->MSTGraph);
			
				float src_freqbydeg = (float)src_freq*20/(float)u_degree;
				float tar_freqbydeg = (float)tar_freq*20/(float)v_degree;*/

			/*	src_prop.insert(pair<string,string>("size",boost::lexical_cast<std::string>(src_freqbydeg)));
				tar_prop.insert(pair<string,string>("size",boost::lexical_cast<std::string>(tar_freqbydeg)));*/
				this->mycurl_graph.add_edge(src,src_prop,tar,tar_prop,edge_prop);
			}
		}
		//delete &path;
		//delete &parent;
	}
	/*delete &src_prop;
	delete &tar_prop;
	delete &edge_prop;*/
	return;
}
void MwadjProcessor :: printTimeSortedData(string opfilename)
{
	std::ofstream opfilepointer;
	opfilepointer.open(opfilename.c_str(),std::ios::app);
	map<double,vector<string> >::iterator time_sort_itr; 
	for ( time_sort_itr = this->time_sorted_mwadj.begin();time_sort_itr != this->time_sorted_mwadj.end();time_sort_itr++)
	{
		std::string linetowrite = boost::lexical_cast<string>(time_sort_itr->first)+":";
		for(int i =0; i<time_sort_itr->second.size();i++)
		{
			linetowrite+=time_sort_itr->second[i]+" ";
		}
		opfilepointer<<linetowrite<<endl;
	}
	opfilepointer.close();
	return;
}
void MwadjProcessor :: processTimeSortedData()
{
	int currtickTime = this->overall_ticks+1;

	vector<int> rank(this->no_of_vertices);
	vector<int> parent(this->no_of_vertices);

	mydisjointSet ds(&rank[0],&parent[0]);
	initialiseDisjointSets(ds);
	
	for ( map<double,vector<string> > ::iterator time_sort_itr = time_sorted_mwadj.begin();time_sort_itr != time_sorted_mwadj.end();time_sort_itr++)
	{
		int currsysFreq = time_sort_itr->second.size();
		vector<int> listofChiVals;
		float sbp = 0.0;
		this->processSystem(currsysFreq,currtickTime,time_sort_itr->first,listofChiVals,sbp);
		bool tempSendFlag = true;
		/*if ( time_sort_itr->first >= this->timetosend)
		{
			tempSendFlag = true;
			this->timetosend+=1;
		}
		else
			tempSendFlag = false;*/
		
		for ( vector<string>::iterator edge_itr = time_sort_itr->second.begin();edge_itr != time_sort_itr->second.end();edge_itr++)
		{
			vector<string> src_tar; 
			boost::split(src_tar,*edge_itr,boost::is_any_of(","));
			int src = atoi(src_tar[0].c_str());
			int tar = atoi(src_tar[1].c_str());
			int edge_id = this->edge_id_mapping.find(*edge_itr)->second;
			this->processEdge(edge_id,currtickTime,time_sort_itr->first,listofChiVals,sbp,src,tar);
			this->processVertex(src,currtickTime,time_sort_itr->first,listofChiVals,sbp);
			this->processVertex(tar,currtickTime,time_sort_itr->first,listofChiVals,sbp);
			this->updateInputGraph(src,tar,true);
			this->updateMST(src,tar,ds,sbp,tempSendFlag);
			
			/*this->processEdge(edge_id);
			this->processVertex(src);
			this->processVertex(tar);
			this->updateMST(src,tar);*/
			//delete &src_tar;

		}
		this->edgemetadetailspointer<<currtickTime<<"\t"<<boost::lexical_cast<std::string>(time_sort_itr->first)<<"\t"<<this->no_of_edge_replacements<<"\t"<<no_of_new_edge_added<<endl;
		currtickTime++;
		time_sort_itr->second.clear();
		//delete &listofChiVals;
	}
	this->overall_ticks = currtickTime;
	/*delete &rank;
	delete &parent;*/
}
void MwadjProcessor :: batchProcess(string ipdir,string startfilename,int numOfFiles)
{
	//DIR *pdir;
	//
	//struct dirent *ent;
	//pdir = opendir(ipdir.c_str());
	//if ( pdir != NULL )
	//{

	//	while ((ent = readdir (pdir)) != NULL) 
	//	{
	//		if( strcmp(ent->d_name, ".") != 0 && strcmp(ent->d_name, "..") != 0 )
	//		{
	//			string filename = ipdir+"\\"+(string)ent->d_name;
	//			this->processinput(filename);
	//			this->processTimeSortedData();
	//		}
	//	}
	//}

	for (int i = 1; i<=numOfFiles;i++)
	{
		string filename = this->ipdir +"\\mwadj\\" +startfilename + boost::lexical_cast<std::string>(i)+ ".mwadj";
		cout << "Processing file : " << filename << endl;
		this->processinput(filename);
		
	}
	this->timetosend = this->time_sorted_mwadj.begin()->first;
	this->processTimeSortedData();
	
	this->printMetaFileDetails(this->ipdir+"\\details_new7.meta");
	//this->checkUniqEdges();
	this->findPlanarXY(this->MSTGraph,this->ipdir+"\\planarXY4.coords");
	//this->printVertexMapping(ipdir+"\\vertex_mapping.txt");
	

}
void MwadjProcessor :: printVertexMapping(string opfilename)
{
	ofstream opfilepointer;
	opfilepointer.open(opfilename.c_str(),ios::app);
	map<int,int>::iterator map_itr;
	for ( map_itr = this->vertex_id_mapping.begin();map_itr!=this->vertex_id_mapping.end();map_itr++)
	{
		opfilepointer<<map_itr->first<<","<<map_itr->second<<endl;
	}
	opfilepointer.close();
	
}
bool MwadjProcessor :: checkUniqEdges()
{
  map<string,bool> uniq_edge_list;
  ofstream edgefilepointer,vertexfilepointer;
  edgefilepointer.open("D:\\snaptwitter1\\Iranelection\\planar2\\edgelist.txt",ios::app);
  graph_traits<Graph>::edge_iterator ei, ei_end;
  for(tie(ei, ei_end) = edges(this->MSTGraph); ei != ei_end; ++ei)
  {
	//cout<<*ei<<endl;
	 int src = (int)source(*ei,this->MSTGraph);
	 int tar = (int)target(*ei,this->MSTGraph);
	 string edge_comb_1 = boost::lexical_cast<std::string>(src)+","+boost::lexical_cast<std::string>(tar);
	 string edge_comb_2 = boost::lexical_cast<std::string>(tar)+","+boost::lexical_cast<std::string>(src);
	 map<string,bool>::iterator comb_itr_1 = uniq_edge_list.find(edge_comb_1);
	 map<string,bool>::iterator comb_itr_2 = uniq_edge_list.find(edge_comb_2);
	 if (( comb_itr_1 != uniq_edge_list.end()) || (comb_itr_2 != uniq_edge_list.end()))
	 {
		 if ( comb_itr_1 != uniq_edge_list.end())
			 comb_itr_1->second = true;
		 else
			 comb_itr_2->second = true;
	 }
	 else
	 {
		uniq_edge_list.insert(pair<string,bool>(edge_comb_1,false));
	 }
	 
  }
  map<string,bool>::iterator list_itr;
  for ( list_itr= uniq_edge_list.begin();list_itr != uniq_edge_list.end();list_itr++)
  {
		edgefilepointer << list_itr->first<<","<<list_itr->second<<";"<<endl;
  }
  edgefilepointer.close();
  return true;
}
void MwadjProcessor :: loadTimeSortedData(string ipfilename,bool isStream)
{
	std::ifstream ipfilepointer;
	ipfilepointer.open(ipfilename.c_str(),ifstream::in);
	string linetoread;
	map<string,bool> temp_edges_present;
	curlWrapper tempcurl;
	while(getline(ipfilepointer,linetoread))
	{
		map<string,bool> temp_edges_to_be_added;
		vector<string> remove_time;
		boost::split(remove_time,linetoread,boost::is_any_of(":"));
		vector<string> edges;
		
		boost::split(edges,remove_time[1],boost::is_any_of(" "));
		double temptimestamp = boost::lexical_cast<double>(remove_time[0]);
		this->time_sorted_mwadj.insert(pair<double,vector<string> >(temptimestamp,edges));
		if (isStream)
		{
			for(int i =0 ; i< edges.size();i++)
			{
				if(edges[i] =="")
					continue;
				vector<string> vertices;
			
				boost::split(vertices,edges[i],boost::is_any_of(","));
				int src= boost::lexical_cast<int>(vertices[0]);
				int	tar = boost::lexical_cast<int>(vertices[1]);
				boost::add_edge(src,tar,this->inputGraph);
				boost::graph_traits<Graph>::vertex_descriptor u;
				boost::graph_traits<Graph>::vertex_descriptor v;
				deg_size_type u_degree,v_degree;
				
				u = vertex(src,this->inputGraph);
				v = vertex(tar,this->inputGraph);

				u_degree  = degree(u,this->inputGraph);
				v_degree = degree(v,this->inputGraph);
				map<string,string> src_prop,tar_prop,edge_prop;
				map<int,XYZcoord>::iterator src_xy_itr= this->nodeXYZ.find(src);
				map<int,XYZcoord>::iterator tar_xy_itr= this->nodeXYZ.find(tar);

				src_prop.insert(pair<string,string>("size",boost::lexical_cast<std::string>(u_degree)));
				src_prop.insert(pair<string,string>("x",boost::lexical_cast<std::string>(src_xy_itr->second.X)));
				src_prop.insert(pair<string,string>("y",boost::lexical_cast<std::string>(src_xy_itr->second.Y)));

				tar_prop.insert(pair<string,string>("size",boost::lexical_cast<std::string>(v_degree)));
				tar_prop.insert(pair<string,string>("x",boost::lexical_cast<std::string>(tar_xy_itr->second.X)));
				tar_prop.insert(pair<string,string>("y",boost::lexical_cast<std::string>(tar_xy_itr->second.Y)));

				edge_prop.insert(pair<string,string>("weight","10"));
				edge_prop.insert(pair<string,string>("source",boost::lexical_cast<std::string>(src)));
				edge_prop.insert(pair<string,string>("target",boost::lexical_cast<std::string>(tar)));
				
				
				
				tempcurl.add_edge(src,src_prop,tar,tar_prop,edge_prop);

				
			
			}		
		}
	}
}
void MwadjProcessor :: loadXYZCoordinates(string ipfilename)
{
	std::ifstream ipfilepointer;
	ipfilepointer.open(ipfilename.c_str(),std::ifstream::in);
	string linetoread;
	while(getline(ipfilepointer,linetoread))
	{
		vector<std::string> parts;
		boost::split(parts,linetoread,boost::is_any_of(" "));
		XYZcoord tempcords;
		tempcords.X = boost::lexical_cast<float>(parts[1]);
		tempcords.Y = boost::lexical_cast<float>(parts[2]);
		tempcords.Z =0.0;
		this->nodeXYZ.insert(pair<int,XYZcoord>(boost::lexical_cast<int>(parts[0]),tempcords));
	}
	ipfilepointer.close();
	return;
}
void MwadjProcessor :: readCSVfile(string ipfilename)
{
	map<long,int> id_mapping;
	ifstream ipfilepointer1;
	ipfilepointer1.open("C:\\Documents and Settings\\abello\\Desktop\\Manifest April 20th 2011\\gephi input\\nodemap.txt",ifstream::in);
	string templine;
	while(getline(ipfilepointer1,templine))
	{
		vector<string> parts;
		boost::split(parts,templine,boost::is_any_of(","));
		long oldid = boost::lexical_cast<long>(parts[0]);
		int newid = boost::lexical_cast<int>(parts[1]);
		id_mapping.insert(pair<long,int>(oldid,newid));
	}



	ifstream ipfilepointer;
	ipfilepointer.open(ipfilename.c_str(),ifstream::in);
	string linetoread;
	curlWrapper mycurl;
	mycurl.setopfilename("manifest_selective_label.rec");
	int topEdgesCount = 0;
	while(getline(ipfilepointer,linetoread))
	{
		vector<std::string> parts;
		boost::split(parts,linetoread,boost::is_any_of(";"));
		long tempsrc = boost::lexical_cast<int>(parts[0]);
		long temptar = boost::lexical_cast<int>(parts[1]);
		int src = id_mapping.find(tempsrc)->second;
		int tar = id_mapping.find(temptar)->second;
		float edge_weight = boost::lexical_cast<float>(parts[2]);
		map<string,string> src_prop,tar_prop,edge_prop;
		map<int,XYZcoord>::iterator src_xy_itr= this->nodeXYZ.find(src);
		map<int,XYZcoord>::iterator tar_xy_itr= this->nodeXYZ.find(tar);
		
		map<int,rgb>::iterator src_rgb_itr = this->nodeRGB.find(src);
		map<int,rgb>::iterator tar_rgb_itr = this->nodeRGB.find(tar);

		map<int,string>::iterator src_lab_itr = this->nodeLabel.find(src);
		map<int,string>::iterator tar_lab_itr = this->nodeLabel.find(tar);

		src_prop.insert(pair<string,string>("size","5"));
		src_prop.insert(pair<string,string>("x",boost::lexical_cast<std::string>(src_xy_itr->second.X)));
		src_prop.insert(pair<string,string>("y",boost::lexical_cast<std::string>(src_xy_itr->second.Y)));
		src_prop.insert(pair<string,string>("r",boost::lexical_cast<std::string>((float)src_rgb_itr->second.r/255)));
		src_prop.insert(pair<string,string>("g",boost::lexical_cast<std::string>((float)src_rgb_itr->second.g/255)));
		src_prop.insert(pair<string,string>("b",boost::lexical_cast<std::string>((float)src_rgb_itr->second.b/255)));
		

		tar_prop.insert(pair<string,string>("size","5"));
		tar_prop.insert(pair<string,string>("x",boost::lexical_cast<std::string>(tar_xy_itr->second.X)));
		tar_prop.insert(pair<string,string>("y",boost::lexical_cast<std::string>(tar_xy_itr->second.Y)));
		tar_prop.insert(pair<string,string>("r",boost::lexical_cast<std::string>((float)tar_rgb_itr->second.r/255)));
		tar_prop.insert(pair<string,string>("g",boost::lexical_cast<std::string>((float)tar_rgb_itr->second.g/255)));
		tar_prop.insert(pair<string,string>("b",boost::lexical_cast<std::string>((float)tar_rgb_itr->second.b/255)));
		

		edge_prop.insert(pair<string,string>("weight",boost::lexical_cast<std::string>(edge_weight)));
		edge_prop.insert(pair<string,string>("source",boost::lexical_cast<std::string>(src)));
		edge_prop.insert(pair<string,string>("target",boost::lexical_cast<std::string>(tar)));
		
		if ( topEdgesCount < 121)
		{
			src_prop.insert(pair<string,string>("Label",src_lab_itr->second));
			//tar_prop.insert(pair<string,string>("Label",tar_lab_itr->second));
		}
		/*else
		{
			src_prop.insert(pair<string,string>("Label",""));
			tar_prop.insert(pair<string,string>("Label",""));
		}*/

		mycurl.add_edge(src,src_prop,tar,tar_prop,edge_prop);
		//src_prop.find("Label")->second = "";
		//tar_prop.find("Label")->second ="";
		/*mycurl.change_node(src,src_prop);
		mycurl.change_node(tar,tar_prop);*/
		topEdgesCount++;

	}

}
void MwadjProcessor :: loadRGB(string ipfilename)
{
	std::ifstream ipfilepointer;
	ipfilepointer.open(ipfilename.c_str(),std::ifstream::in);
	string linetoread;
	while(getline(ipfilepointer,linetoread))
	{
		vector<std::string> parts;
		boost::split(parts,linetoread,boost::is_any_of(" "));
		rgb tempRGB;
		tempRGB.r = boost::lexical_cast<int>(parts[1]);
		tempRGB.g = boost::lexical_cast<int>(parts[2]);
		tempRGB.b = boost::lexical_cast<int>(parts[3]);
		this->nodeRGB.insert(pair<int,rgb>(boost::lexical_cast<int>(parts[0]),tempRGB));
	}
	ipfilepointer.close();
	return;
}
void MwadjProcessor :: readVertexCSVfile(string ipfilename)
{
	std::ifstream ipfilepointer;
	ipfilepointer.open(ipfilename.c_str(),std::ifstream::in);
	string linetoread;
	while(getline(ipfilepointer,linetoread))
	{
		vector<std::string> parts;
		boost::split(parts,linetoread,boost::is_any_of(";"));
		int id = boost::lexical_cast<int>(parts[0]);
		this->nodeLabel.insert(pair<int,string>(id,parts[1]));
	}
	return;
}
void printVertexlabels(string opfilename)
{
	ofstream opfilepointer;
	opfilepointer.open(opfilename.c_str(),ios::app);

}
void MwadjProcessor :: printMetaFileDetails(string opfilename)
{
	map<double,mySystem>::reverse_iterator sys_itr = this->system_properties.rbegin();
	
	ofstream opfilepointer;
	opfilepointer.open(opfilename.c_str(),ios::app);
	
	opfilepointer << "time taken : " << clock()/CLOCKS_PER_SEC <<" secs" << endl;
	opfilepointer<<"system frequency:"<<sys_itr->second.freq<<endl;
	opfilepointer<<"No of vertices: "<<this->no_of_vertices<<endl;
	opfilepointer << "No of edges: " << this->no_of_edges<<endl;
	opfilepointer<<"Max chi Vertex :" << this->max_chi_vertex<<endl;
	opfilepointer<<"Min chi vertex : " << this->min_chi_vertex<<endl;
	opfilepointer << "Max chi Edge : " << this->max_chi_edge<<endl;
	opfilepointer << " Min Chi Edge : " << this->min_chi_edge<<endl;
	opfilepointer << " Max freq vertex : " << this->max_freq_vertex <<endl;
	opfilepointer << "Min Freq vertex :" << this->min_freq_vertex<<endl;
	opfilepointer << "Max freq edge : " << this->max_freq_edge<<endl;
	opfilepointer << "Min Freq edge : " << this->min_freq_edge<<endl;
	opfilepointer << "Max sbp vertex : " << this->max_sbp_vertex<<endl;
	opfilepointer << " min sbp vertex : " << this->min_sbp_vertex<<endl;
	opfilepointer <<"max sbp edge :"<< this->max_sbp_edge<<endl;
	opfilepointer << " min sbp edge : " << this->min_sbp_edge<<endl;


	opfilepointer.close();

}
void MwadjProcessor :: printEdgeDetails(string opfilename)
{
	ofstream opfilepointer;
	opfilepointer.open(opfilename.c_str(),ios::app);
	map<int,myEdge>::iterator edge_itr ;
	for ( edge_itr = this->edge_properties.begin();edge_itr != this->edge_properties.end();edge_itr++)
	{
		opfilepointer<<edge_itr->first<<"[ chi_val = \"" << edge_itr->second.chi_val <<"\" freq = \"" << edge_itr->second.freq << "\"];";
	}
	opfilepointer.close();
	return;
}
void generateGraph(int i,int j,int k,int currlevel,int maxlevel,int& global_max,ofstream& opfilepointer)
{
	
	if ( currlevel >maxlevel)
		return;
	int center = global_max;
	
	opfilepointer << i<<" " << center <<endl;
	opfilepointer << j<<" " << center<<endl;
	opfilepointer << k<<" " << center<<endl;
	++global_max;
	//count = count+(int)pow(3.0,level);
	++currlevel;
	generateGraph(i,j,center,currlevel,maxlevel,global_max,opfilepointer);
	generateGraph(j,k,center,currlevel,maxlevel,global_max,opfilepointer);
	generateGraph(k,i,center,currlevel,maxlevel,global_max,opfilepointer);
	
	return;

}
void testPlanar2()
{
		 typedef adjacency_list
    < vecS,
      vecS,
      undirectedS,
      property<vertex_index_t, int>,
      property<edge_index_t, int>
    > 
    Graph;
 map<string,bool> edge_list;
 curlWrapper mycurl;
 mycurl.setopfilename("events_testmst7_new.rec");
 map<long,int> id_mapping;
 ifstream ipfilepointer1;
 /*ipfilepointer1.open("C:\\Documents and Settings\\abello\\Desktop\\Manifest April 20th 2011\\gephi input\\nodemap.txt",ifstream::in);
 string templine;
 while(getline(ipfilepointer1,templine))
 {
	 vector<string> parts;
	 boost::split(parts,templine,boost::is_any_of(","));
	 long oldid = boost::lexical_cast<long>(parts[0]);
	 int newid = boost::lexical_cast<int>(parts[1]);
	 id_mapping.insert(pair<long,int>(oldid,newid));
 }*/
 ifstream ipfilepointer;
 ipfilepointer.open("D:\\RA1\\planar5\\test cases\\inputGraph_del_1_page.txt",ifstream::in);
 string linetoread;
 Graph tempGraph;
 ofstream tempfilepointer;
 tempfilepointer.open("D:\\RA1\\planar5\\test cases\\iptoDS_9_1.txt",ios::app);
 int num_of_lines = 0;
 int i = 1;
 while(getline(ipfilepointer,linetoread))
 {
	
	 map<string,string> edge_prop,src_prop,tar_prop;
	 vector<string> parts;
	 boost::split(parts,linetoread,boost::is_any_of(" "));
	 long tempsrc = boost::lexical_cast<long>(parts[0]);
	 long temptar = boost::lexical_cast<long>(parts[1]);
	 //parts[0] = boost::lexical_cast<std::string>(id_mapping.find(tempsrc)->second);
	 //parts[1] = boost::lexical_cast<std::string>(id_mapping.find(temptar)->second);
	 string altEdgeComb = parts[1]+" "+parts[0];
	 if ( edge_list.find(linetoread) !=edge_list.end())
		 continue;
	 if ( edge_list.find(altEdgeComb) != edge_list.end())
		 continue;
	 edge_list.insert(pair<string,bool>(linetoread,true));
	 int src = boost::lexical_cast<int>(parts[0]);
	 int tar = boost::lexical_cast<int>(parts[1]);
	 add_edge(src,tar,tempGraph);
	 tempfilepointer << src<< " " <<tar<<endl;
	 if ( num_of_lines == 1000)
	 {
		 cout<<"No of lines read : " << i*num_of_lines << endl;
		 i++;
		 num_of_lines = 0;
	 }
	 num_of_lines++;
	 edge_prop.insert(pair<string,string>("source",parts[0]));
	 edge_prop.insert(pair<string,string>("target",parts[1]));
	 mycurl.add_edge(src,src_prop,tar,tar_prop,edge_prop);
 };
 tempfilepointer.close();
 // 
 // add_edge(0,1,tempGraph);
 // edge_prop.insert(pair<string,string>("source","0"));
 // edge_prop.insert(pair<string,string>("target","1"));
 // mycurl.add_edge(0,src_prop,1,tar_prop,edge_prop);\

 // add_edge(1,2,tempGraph);
 // edge_prop.find("source")->second = "1";
 // edge_prop.find("target")->second = "2";
 // mycurl.add_edge(1,src_prop,2,tar_prop,edge_prop);

 // add_edge(2,3,tempGraph);
 // edge_prop.find("source")->second = "2";
 // edge_prop.find("target")->second = "3";
 // mycurl.add_edge(2,src_prop,3,tar_prop,edge_prop);

 ///* add_edge(3,0,tempGraph);
 // edge_prop.find("source")->second = "3";
 // edge_prop.find("target")->second = "0";
 // mycurl.add_edge(3,src_prop,0,tar_prop,edge_prop);*/

 // /* add_edge(1,3,tempGraph);
 // edge_prop.find("source")->second = "1";
 // edge_prop.find("target")->second = "3";
 // mycurl.add_edge(1,src_prop,3,tar_prop,edge_prop);*/

 //  add_edge(0,4,tempGraph);
 // edge_prop.find("source")->second = "0";
 // edge_prop.find("target")->second = "4";
 // mycurl.add_edge(0,src_prop,4,tar_prop,edge_prop);

 // /*   add_edge(0,5,tempGraph);
 // edge_prop.find("source")->second = "0";
 // edge_prop.find("target")->second = "5";
 // mycurl.add_edge(0,src_prop,5,tar_prop,edge_prop);*/

 //    add_edge(4,5,tempGraph);
 // edge_prop.find("source")->second = "4";
 // edge_prop.find("target")->second = "5";
 // mycurl.add_edge(4,src_prop,5,tar_prop,edge_prop);


 //   add_edge(5,1,tempGraph);
 // edge_prop.find("source")->second = "5";
 // edge_prop.find("target")->second = "1";
 // mycurl.add_edge(5,src_prop,1,tar_prop,edge_prop);


 // add_edge(5,2,tempGraph);
 // edge_prop.find("source")->second = "5";
 // edge_prop.find("target")->second = "2";
 // mycurl.add_edge(5,src_prop,2,tar_prop,edge_prop);


 // add_edge(5,3,tempGraph);
 // edge_prop.find("source")->second = "5";
 // edge_prop.find("target")->second = "3";
 // mycurl.add_edge(5,src_prop,3,tar_prop,edge_prop);

 // 
 // add_edge(4,3,tempGraph);
 // edge_prop.find("source")->second = "4";
 // edge_prop.find("target")->second = "3";
 // mycurl.add_edge(4,src_prop,3,tar_prop,edge_prop);


 // /*add_edge(4,5,g);
 // add_edge(5,3,g);
 // add_edge(5,6,g);
 // add_edge(6,7,g);
 // add_edge(7,8,g);
 // add_edge(8,5,g);
 // add_edge(8,9,g);
 // add_edge(0,10,g);*/
	struct planar_coord
	{
		std::size_t x;
		std::size_t y;
	};
	ofstream opfilepointer;
	ofstream metafilepointer;
	string metafilename = "D:\\RA1\\planar5\\test cases\\details_9_1.meta";
	string opfilename = "D:\\RA1\\planar5\\test cases\\details_9_1XY.coords";
	opfilepointer.open(opfilename.c_str(),ios::app);
	metafilepointer.open(metafilename.c_str(),ios::app);
	metafilepointer<<"no of vertices:"<<num_vertices(tempGraph)<<endl;
	metafilepointer<<"no of edges : " <<num_edges(tempGraph)<<endl;
	metafilepointer.close();
	  std::vector< graph_traits<Graph>::vertices_size_type > 
    component(num_vertices(tempGraph));
	std::cout << "Before calling make_connected, the graph has "
            << connected_components(tempGraph, &component[0])
            << " connected components" << std::endl;


	make_connected(tempGraph);

	std::cout << "After calling make_connected, the graph has "
            << connected_components(tempGraph, &component[0])
            << " connected components" << std::endl;

  //Initialize the interior edge index
  property_map<Graph, edge_index_t>::type e_index = get(edge_index, tempGraph);
  graph_traits<Graph>::edges_size_type edge_count = 0;
  graph_traits<Graph>::edge_iterator ei, ei_end;
  for(tie(ei, ei_end) = edges(tempGraph); ei != ei_end; ++ei)
    put(e_index, *ei, edge_count++);




	 //Define the storage type for the planar embedding
	typedef std::vector< std::vector< graph_traits<Graph>::edge_descriptor > > 
		embedding_storage_t;
	typedef boost::iterator_property_map
	 < embedding_storage_t::iterator, 
		 property_map<Graph, vertex_index_t>::type 
	 >
	 embedding_t;

	 
	// Create the planar embedding
	embedding_storage_t embedding_storage(num_vertices(tempGraph));
	embedding_t embedding(embedding_storage.begin(), get(vertex_index,tempGraph));
	
	bool isplanar = boyer_myrvold_planarity_test(boyer_myrvold_params::graph = tempGraph,
                               boyer_myrvold_params::embedding = &embedding[0]
                               );
	
	  //typedef typename property_traits<PlanarEmbedding>::value_type 
   //   embedding_value_t;
   // typedef typename embedding_value_t::const_iterator embedding_iterator_t;


    typedef std::vector< graph_traits<Graph>::edges_size_type > 
    component_storage_t;
	typedef iterator_property_map
    < component_storage_t::iterator, 
      property_map<Graph, edge_index_t>::type
    >
    component_map_t;
  
	component_storage_t component_storage(num_edges(tempGraph));
	component_map_t component1(component_storage.begin(), get(edge_index, tempGraph));
  
	std::cout << "Before calling make_biconnected_planar, the graph has "
            << biconnected_components(tempGraph, component1)
            << " biconnected components" << std::endl;

	make_biconnected_planar(tempGraph,embedding);
	
	// Re-initialize the edge index, since we just added a few edges
	 edge_count = 0;
	for(tie(ei, ei_end) = edges(tempGraph); ei != ei_end; ++ei)
	 put(e_index, *ei, edge_count++);

	 // Re-size the storage for the biconnected components, since we
	 // just added a few edges
  
	component_storage.resize(num_edges(tempGraph));
	component1 = component_map_t(component_storage.begin(), get(edge_index,tempGraph));

	std::cout << "After calling make_biconnected_planar, the graph has "
            << biconnected_components(tempGraph, component1)
            << " biconnected components" << std::endl;
	char temp;
	cin >> temp;
	embedding_storage_t embedding_storage1(num_vertices(tempGraph));
	embedding_t embedding1(embedding_storage1.begin(), get(vertex_index,tempGraph));
	isplanar = boyer_myrvold_planarity_test(boyer_myrvold_params::graph = tempGraph,
                               boyer_myrvold_params::embedding = &embedding1[0]
                               );
	make_maximal_planar(tempGraph,embedding1);
	embedding_storage_t embedding_storage2(num_vertices(tempGraph));
	embedding_t embedding2(embedding_storage2.begin(), get(vertex_index,tempGraph));
	isplanar = boyer_myrvold_planarity_test(boyer_myrvold_params::graph = tempGraph,
                               boyer_myrvold_params::embedding = &embedding2[0]
                               );
	// Find a canonical ordering
	typedef std::vector<graph_traits<Graph>::vertex_descriptor> ordering_storage_t;
	ordering_storage_t ordering;

	planar_canonical_ordering(tempGraph, embedding2, std::back_inserter(ordering));
	ordering_storage_t::iterator oi;
	for ( oi = ordering.begin();oi!=ordering.end();oi++)
		cout<<*oi<<endl;
	
	//Set up a property map to hold the mapping from vertices to coord_t's
	typedef std::vector< planar_coord > straight_line_drawing_storage_t;
	typedef boost::iterator_property_map
		<straight_line_drawing_storage_t::iterator,property_map<Graph, vertex_index_t>::type> straight_line_drawing_t;

	straight_line_drawing_storage_t straight_line_drawing_storage(num_vertices(tempGraph));
	straight_line_drawing_t straight_line_drawing(straight_line_drawing_storage.begin(),get(vertex_index,tempGraph));

	chrobak_payne_straight_line_drawing(tempGraph, 
                                      embedding2, 
                                      ordering.begin(),
                                      ordering.end(),
                                      straight_line_drawing
                                      );


	graph_traits<Graph>::vertex_iterator vi, vi_end;
	for(tie(vi,vi_end) = vertices(tempGraph); vi != vi_end; ++vi)
    {
      planar_coord coord(get(straight_line_drawing,*vi));
      //std::cout << *vi << " -> (" << coord.x << ", " << coord.y << ")" << std::endl;
	  opfilepointer << *vi << " " << coord.x << " " << coord.y << endl;
    }

	// Verify that the drawing is actually a plane drawing
	if (is_straight_line_drawing(tempGraph, straight_line_drawing))
	 std::cout << "Is a plane drawing." << std::endl;
	 else
	 std::cout << "Is not a plane drawing." << std::endl;
	char ch;
	cin >> ch;
	opfilepointer.close();
	mycurl.closeOfStream();
	return;
}
void MwadjProcessor::findPlanarXY(Graph tempGraph, string opfilename)
{
	struct planar_coord
	{
		std::size_t x;
		std::size_t y;
	};
	ofstream opfilepointer;
	opfilepointer.open(opfilename.c_str(),ios::app);
	  std::vector< graph_traits<Graph>::vertices_size_type > 
    component(num_vertices(tempGraph));
	std::cout << "Before calling make_connected, the graph has "
            << connected_components(tempGraph, &component[0])
            << " connected components" << std::endl;


	make_connected(tempGraph);

	std::cout << "After calling make_connected, the graph has "
            << connected_components(tempGraph, &component[0])
            << " connected components" << std::endl;
	
  //Initialize the interior edge index
  property_map<Graph, edge_index_t>::type e_index = get(edge_index, tempGraph);
  graph_traits<Graph>::edges_size_type edge_count = 0;
  graph_traits<Graph>::edge_iterator ei, ei_end;
  for(tie(ei, ei_end) = edges(tempGraph); ei != ei_end; ++ei)
    put(e_index, *ei, edge_count++);




	 //Define the storage type for the planar embedding
	typedef std::vector< std::vector< graph_traits<Graph>::edge_descriptor > > 
		embedding_storage_t;
	typedef boost::iterator_property_map
	 < embedding_storage_t::iterator, 
		 property_map<Graph, vertex_index_t>::type 
	 >
	 embedding_t;

	 
	// Create the planar embedding
	embedding_storage_t embedding_storage(num_vertices(tempGraph));
	embedding_t embedding(embedding_storage.begin(), get(vertex_index,tempGraph));
	
	bool isplanar = boyer_myrvold_planarity_test(boyer_myrvold_params::graph = tempGraph,
                               boyer_myrvold_params::embedding = &embedding[0]
                               );
	
	  //typedef typename property_traits<PlanarEmbedding>::value_type 
   //   embedding_value_t;
   // typedef typename embedding_value_t::const_iterator embedding_iterator_t;


    typedef std::vector< graph_traits<Graph>::edges_size_type > 
    component_storage_t;
	typedef iterator_property_map
    < component_storage_t::iterator, 
      property_map<Graph, edge_index_t>::type
    >
    component_map_t;
  
	component_storage_t component_storage(num_edges(tempGraph));
	component_map_t component1(component_storage.begin(), get(edge_index, tempGraph));
  
	std::cout << "Before calling make_biconnected_planar, the graph has "
            << biconnected_components(tempGraph, component1)
            << " biconnected components" << std::endl;

	make_biconnected_planar(tempGraph,embedding);
	
	// Re-initialize the edge index, since we just added a few edges
	 edge_count = 0;
	for(tie(ei, ei_end) = edges(tempGraph); ei != ei_end; ++ei)
	 put(e_index, *ei, edge_count++);

	 // Re-size the storage for the biconnected components, since we
	 // just added a few edges
  
	component_storage.resize(num_edges(tempGraph));
	component1 = component_map_t(component_storage.begin(), get(edge_index,tempGraph));

	std::cout << "After calling make_biconnected_planar, the graph has "
            << biconnected_components(tempGraph, component1)
            << " biconnected components" << std::endl;

	embedding_storage_t embedding_storage1(num_vertices(tempGraph));
	embedding_t embedding1(embedding_storage1.begin(), get(vertex_index,tempGraph));
	isplanar = boyer_myrvold_planarity_test(boyer_myrvold_params::graph = tempGraph,
                               boyer_myrvold_params::embedding = &embedding1[0]
                               );
	make_maximal_planar(tempGraph,embedding1);
	embedding_storage_t embedding_storage2(num_vertices(tempGraph));
	embedding_t embedding2(embedding_storage2.begin(), get(vertex_index,tempGraph));
	isplanar = boyer_myrvold_planarity_test(boyer_myrvold_params::graph = tempGraph,
                               boyer_myrvold_params::embedding = &embedding2[0]
                               );
	// Find a canonical ordering
	typedef std::vector<graph_traits<Graph>::vertex_descriptor> ordering_storage_t;
	ordering_storage_t ordering;

	planar_canonical_ordering(tempGraph, embedding2, std::back_inserter(ordering));
	ordering_storage_t::iterator oi;
	for ( oi = ordering.begin();oi!=ordering.end();oi++)
		cout<<*oi<<endl;
	
	//Set up a property map to hold the mapping from vertices to coord_t's
	typedef std::vector< planar_coord > straight_line_drawing_storage_t;
	typedef boost::iterator_property_map
		<straight_line_drawing_storage_t::iterator,property_map<Graph, vertex_index_t>::type> straight_line_drawing_t;

	straight_line_drawing_storage_t straight_line_drawing_storage(num_vertices(tempGraph));
	straight_line_drawing_t straight_line_drawing(straight_line_drawing_storage.begin(),get(vertex_index,tempGraph));

	chrobak_payne_straight_line_drawing(tempGraph, 
                                      embedding2, 
                                      ordering.begin(),
                                      ordering.end(),
                                      straight_line_drawing
                                      );


	graph_traits<Graph>::vertex_iterator vi, vi_end;
	for(tie(vi,vi_end) = vertices(tempGraph); vi != vi_end; ++vi)
    {
      planar_coord coord(get(straight_line_drawing,*vi));
      //std::cout << *vi << " -> (" << coord.x << ", " << coord.y << ")" << std::endl;
	  opfilepointer << *vi << " " << coord.x << " " << coord.y << endl;
    }

	// Verify that the drawing is actually a plane drawing
	//if (is_straight_line_drawing(tempGraph, straight_line_drawing))
	// std::cout << "Is a plane drawing." << std::endl;
	// else
	// std::cout << "Is not a plane drawing." << std::endl;
	char ch;
	cin >> ch;
	opfilepointer.close();
	return;

}
string MwadjProcessor::getIpDir()
{
	return this->ipdir;
}
bool MwadjProcessor :: setIpDir(string ipdir)
{
	this->ipdir = ipdir;
	return true;
}
int main(int argc, char* argv[])
{
	
	
	string tempipdir = "";// "D:\\snaptwitter1\\nohashtag\\mwadj";
	tempipdir.append(argv[1]);
	string tempfilenames = "";
	tempfilenames.append(argv[2]);
	string nooffiles = "";
	nooffiles.append(argv[3]);
	int numFiles = boost::lexical_cast<int>(nooffiles);
	MwadjProcessor myInstance;
	myInstance.setIpDir(tempipdir);
	//myInstance.readVertexCSVfile("D:\\snaptwitter1\\cooccurence\\nodelabel.csv");
	myInstance.batchProcess(tempipdir,tempfilenames,numFiles);
	//myInstance.printEdgeDetails("Y:\\snaptwitter1\\Iranelection\\edgedetails.txt");
	//myInstance.loadXYZCoordinates("D:\\RA\\XY.cords");
	/*myInstance.processinput(tempfileName);
	myInstance.processTimeSortedData();*/
	//myInstance.printTimeSortedData("D:\\RA\\timesorted_twitter");
	
	////myInstance.loadTimeSortedData("D:\\RA\\timesortedtest.twadj",true);
	//ofstream opfilepointer;
	//opfilepointer.open("D:\\RA1\\planar5\\inputGraph.txt",ios::app);
	//int global_max = 3;
	////int final_count = 0;
	///*for (int n = 0;n<=2;n++)
	//	final_count+=(int)pow(3.0,n);*/
	//opfilepointer <<"0 1"<<endl;
	//opfilepointer <<"1 2" <<endl;
	//opfilepointer <<"2 0"<<endl;
	//generateGraph(0,1,2,0,9,global_max,opfilepointer);
	//opfilepointer.close();
	//testPlanar2();
	//curlWrapper mycurl;
	//mycurl.loadXYZcoordinates("D:\\snaptwitter1\\cooccurence\\planarXY4.coords");
	//mycurl.read_events_from_file("D:\\RA1\\planar5\\events_testmst.rec");
	//mycurl.read_events_from_file("D:\\RA1\\planar5\\events_testmst.rec");
	//mycurl.read_events_from_file("C:\\Documents and Settings\\abello\\Desktop\\Manifest April 20th 2011\\gephi input\\manifest.rec");
	//testPlanar2();


	///// this is specially for manifest data
	/*MwadjProcessor myInstance;
	myInstance.loadXYZCoordinates("C:\\Documents and Settings\\abello\\Desktop\\Manifest April 20th 2011\\gephi input\\tree\\XYlabelsrc.cords");
	myInstance.loadRGB("C:\\Documents and Settings\\abello\\Desktop\\Manifest April 20th 2011\\gephi input\\tree\\color.txt");
	myInstance.readVertexCSVfile("C:\\Documents and Settings\\abello\\Desktop\\Manifest April 20th 2011\\gephi input\\tree\\nodelabel.csv");
	myInstance.readCSVfile("C:\\Documents and Settings\\abello\\Desktop\\Manifest April 20th 2011\\gephi input\\tree\\edges_tree_1.csv");*/
}
